﻿
namespace UserDefinedForms.Windows.Forms
{
	/// <summary>
	/// abstract SupOrderForm typed datagridview
	/// </summary>
	[System.ComponentModel.DesignerCategoryAttribute("code")]
	public abstract class SupOrderFormDataGridViewBase : System.Windows.Forms.UserControl
	{	
		/// <summary> 
		/// Thrown when the current Entity changed.
		/// </summary>
		public event System.EventHandler<SupOrderFormDataGridViewEventArgs> CurrentEntityChanged;
		
		#region Fields
				
		/// <summary> 
		/// the current entity.
		/// </summary>
		private Entities.SupOrderForm _currentSupOrderForm = null;
		//private Dictionary<string, string> _columnsHeaders = new Dictionary<string, string>();
		
		/// <summary> 
		/// the datagridview instance
		/// </summary>
		protected System.Windows.Forms.DataGridView uxSupOrderFormDataGridView;
		
		/// <summary> 
		/// the errorprovider
		/// </summary>
		protected System.Windows.Forms.ErrorProvider uxSupOrderFormErrorProvider;
		
		/// <summary> 
		/// the bindingsource
		/// </summary>
		protected System.Windows.Forms.BindingSource uxSupOrderFormBindingSource;
		
		/// <summary> 
		/// the DGV column associated with the FormId property
		/// </summary>
		protected System.Windows.Forms.DataGridViewComboBoxColumn uxFormIdDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the Background property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxBackgroundDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the SupplierId property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxSupplierIdDataGridViewColumn;
		
		#endregion
					
		#region Combo list
		
				
		private Entities.TList<Entities.DesignForm> _FormIdList;
		
		/// <summary> 
		/// The list of selectable DesignForm
		/// </summary>
		[System.ComponentModel.Category("DropDownList BindingSources")]
		public Entities.TList<Entities.DesignForm> FormIdList
		{
			get {return this._FormIdList;}
			set 
			{
				this._FormIdList = value;
				this.uxFormIdDataGridViewColumn.DataSource = null;
				this.uxFormIdDataGridViewColumn.DataSource = this._FormIdList;
			}
		}
		
		private bool _allowNewItemInFormIdList;
		
		/// <summary> 
		/// Indicates if user can add an item in the list of DesignForm
		/// </summary>
		[System.ComponentModel.Category(".netTiers"), System.ComponentModel.Description("Indicates if end user can add a new item in the combobox. an event will be thrown then.")]
		public bool AllowNewItemInFormIdList
		{
			get { return _allowNewItemInFormIdList;}
			set
			{
				this._allowNewItemInFormIdList = value;
				this.uxFormIdDataGridViewColumn.DisplayStyle = value ? System.Windows.Forms.DataGridViewComboBoxDisplayStyle.ComboBox : System.Windows.Forms.DataGridViewComboBoxDisplayStyle.DropDownButton;
			}
		}
		
		#endregion
		
		#region Main Datasource
		
		private Entities.TList<Entities.SupOrderForm> _SupOrderFormList;
				
		/// <summary> 
		/// The list of SupOrderForm to be displayed in the DGV.
		/// </summary>
		[System.ComponentModel.Category("Data")]
		public Entities.TList<Entities.SupOrderForm> SupOrderFormList
		{
			get {return this._SupOrderFormList;}
			set
			{
				this._SupOrderFormList = value;
				this.uxSupOrderFormBindingSource.DataSource = null;
				this.uxSupOrderFormBindingSource.DataSource = value;
				this.uxSupOrderFormDataGridView.DataSource = null;
				this.uxSupOrderFormDataGridView.DataSource = this.uxSupOrderFormBindingSource;				
				//this.uxSupOrderFormBindingSource.ListChanged += new System.ComponentModel.ListChangedEventHandler(uxSupOrderFormBindingSource_ListChanged);
				this.uxSupOrderFormBindingSource.CurrentItemChanged += new System.EventHandler(OnSupOrderFormBindingSourceCurrentItemChanged);
			}
		}
		
		private void OnSupOrderFormBindingSourceCurrentItemChanged(object sender, System.EventArgs e)
		{
			_currentSupOrderForm = uxSupOrderFormBindingSource.Current as Entities.SupOrderForm;
			
			if (_currentSupOrderForm != null)
			{
				_currentSupOrderForm.Validate();
			}
			//_SupOrderForm.Validate();
			OnCurrentEntityChanged();
		}

		//void uxSupOrderFormBindingSource_ListChanged(object sender, System.ComponentModel.ListChangedEventArgs e)
		//{
			//throw new Exception("The method or operation is not implemented.");
		//}		
		
		#endregion
		
		/// <summary>
		/// Gets the currently selected <see cref="Entities.SupOrderForm"/> instance.
		/// </summary>
		public Entities.SupOrderForm SelectedSupOrderForm
		{
			get {return this._currentSupOrderForm;}
		}
		
		#region other properties
		
		/// <summary>
		/// Gets or sets a value indicating whether you have provided your own data-management operations for the DataGridView control.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool VirtualMode
		{
			get {return this.uxSupOrderFormDataGridView.VirtualMode;}
			set
			{
				this.uxSupOrderFormDataGridView.VirtualMode = value;
			}
		}
		
		/// <summary>
		/// Gets or sets a value indicating whether the option to add rows is displayed to the user.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool AllowUserToAddRows
		{
			get {return this.uxSupOrderFormDataGridView.AllowUserToAddRows;}
			set {this.uxSupOrderFormDataGridView.AllowUserToAddRows = value;}
		}
		
		/// <summary>
		/// Gets or sets a value indicating whether the user is allowed to delete rows from the DataGridView.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool AllowUserToDeleteRows
		{
			get {return this.uxSupOrderFormDataGridView.AllowUserToDeleteRows;}
			set {this.uxSupOrderFormDataGridView.AllowUserToDeleteRows = value;}
		}
		
		/// <summary>
		/// Gets the <see cref="DataGridView"/> instance.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public System.Windows.Forms.DataGridView DataGridView
		{
			get {return this.uxSupOrderFormDataGridView;}
		}
		
		/// <summary>
		/// Gets a collection that contains all the columns in the control.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]		
		[System.ComponentModel.DesignerSerializationVisibilityAttribute(System.ComponentModel.DesignerSerializationVisibility.Visible)]
		//[System.ComponentModel.EditorAttribute("System.Windows.Forms.Design.DataGridViewColumnCollectionEditor, System.Design", typeof(System.Drawing.Design.UITypeEditor))] 
		public System.Windows.Forms.DataGridViewColumnCollection Columns
		{
			get { return this.uxSupOrderFormDataGridView.Columns; }
		}
		
		#endregion
		
		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.IContainer components = null;
	
		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
		protected override void Dispose(bool disposing)
		{
			if (disposing && (components != null))
			{
				components.Dispose();
			}
			base.Dispose(disposing);
		}
	
		#region Constructor
	
		/// <summary>
		/// Initializes a new instance of the <see cref="SupOrderFormDataGridViewBase"/> class.
		/// </summary>
		public SupOrderFormDataGridViewBase()
		{			
			this.components = new System.ComponentModel.Container();			
			
			this.uxSupOrderFormDataGridView = new System.Windows.Forms.DataGridView();
			this.uxSupOrderFormBindingSource = new System.Windows.Forms.BindingSource(this.components);
			this.uxSupOrderFormErrorProvider = new System.Windows.Forms.ErrorProvider(this.components);
			this.uxFormIdDataGridViewColumn = new System.Windows.Forms.DataGridViewComboBoxColumn();
			this.uxBackgroundDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxSupplierIdDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			//this.uxFormIdBindingSource = new DesignFormBindingSource(this.components);
			//((System.ComponentModel.ISupportInitialize)(this.uxFormIdBindingSource)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxSupOrderFormDataGridView)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxSupOrderFormBindingSource)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxSupOrderFormErrorProvider)).BeginInit();
			this.SuspendLayout();
						
			// 
			// uxSupOrderFormErrorProvider
			// 
			this.uxSupOrderFormErrorProvider.ContainerControl = this;
			this.uxSupOrderFormErrorProvider.DataSource = this.uxSupOrderFormBindingSource;						
			// 
			// uxSupOrderFormDataGridView
			// 
			this.uxSupOrderFormDataGridView.AutoGenerateColumns = false;
			this.uxSupOrderFormDataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
			this.uxSupOrderFormDataGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
		this.uxFormIdDataGridViewColumn,
		this.uxBackgroundDataGridViewColumn,
		this.uxSupplierIdDataGridViewColumn			});
			this.uxSupOrderFormDataGridView.Dock = System.Windows.Forms.DockStyle.Fill;
			this.uxSupOrderFormDataGridView.Location = new System.Drawing.Point(0, 0);
			this.uxSupOrderFormDataGridView.Name = "uxSupOrderFormDataGridView";
			this.uxSupOrderFormDataGridView.Size = new System.Drawing.Size(470, 300);
			this.uxSupOrderFormDataGridView.TabIndex = 0;	
			this.uxSupOrderFormDataGridView.BackgroundColor = System.Drawing.Color.WhiteSmoke;
			this.uxSupOrderFormDataGridView.EnableHeadersVisualStyles = false;
			this.uxSupOrderFormDataGridView.DataError += new System.Windows.Forms.DataGridViewDataErrorEventHandler(this.OnSupOrderFormDataGridViewDataError);
			this.uxSupOrderFormDataGridView.CellValueNeeded += new System.Windows.Forms.DataGridViewCellValueEventHandler(this.OnSupOrderFormDataGridViewCellValueNeeded);
			this.uxSupOrderFormDataGridView.CellValuePushed += new System.Windows.Forms.DataGridViewCellValueEventHandler(this.OnSupOrderFormDataGridViewCellValuePushed);
			
			//
			// uxFormIdDataGridViewColumn
			//
			this.uxFormIdDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxFormIdDataGridViewColumn.DataPropertyName = "FormId";
			this.uxFormIdDataGridViewColumn.HeaderText = "FormId";
			this.uxFormIdDataGridViewColumn.Name = "uxFormIdDataGridViewColumn";
			this.uxFormIdDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxFormIdDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxFormIdDataGridViewColumn.ReadOnly = false;		
			//
			// uxBackgroundDataGridViewColumn
			//
			this.uxBackgroundDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxBackgroundDataGridViewColumn.DataPropertyName = "Background";
			this.uxBackgroundDataGridViewColumn.HeaderText = "Background";
			this.uxBackgroundDataGridViewColumn.Name = "uxBackgroundDataGridViewColumn";
			this.uxBackgroundDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxBackgroundDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxBackgroundDataGridViewColumn.ReadOnly = false;		
			//
			// uxSupplierIdDataGridViewColumn
			//
			this.uxSupplierIdDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxSupplierIdDataGridViewColumn.DataPropertyName = "SupplierId";
			this.uxSupplierIdDataGridViewColumn.HeaderText = "SupplierId";
			this.uxSupplierIdDataGridViewColumn.Name = "uxSupplierIdDataGridViewColumn";
			this.uxSupplierIdDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxSupplierIdDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxSupplierIdDataGridViewColumn.ReadOnly = false;		
			//
			// uxFormIdDataGridViewColumn
			//				
			this.uxFormIdDataGridViewColumn.DisplayMember = "Name";	
			this.uxFormIdDataGridViewColumn.ValueMember = "FormId";	
			this.uxFormIdDataGridViewColumn.DisplayStyleForCurrentCellOnly = true;
			//uxFormIdDataGridViewColumn.DataSource = uxFormIdBindingSource;				
				
			
			// this
			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.Controls.Add(this.uxSupOrderFormDataGridView);
			this.Name = "SupOrderFormDataGridView";
			this.Size = new System.Drawing.Size(470, 300);
			//((System.ComponentModel.ISupportInitialize)(this.uxFormIdBindingSource)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxSupOrderFormErrorProvider)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxSupOrderFormDataGridView)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxSupOrderFormBindingSource)).EndInit();
			this.ResumeLayout(false);
		}
		#endregion
				
		#region events
		
		/// <summary>
		/// Raised the CurrentEntityChanged event.
		/// </summary>
		protected void OnCurrentEntityChanged()
		{
			if (CurrentEntityChanged != null)
			{
				SupOrderFormDataGridViewEventArgs args = new SupOrderFormDataGridViewEventArgs();
				args.SupOrderForm = _currentSupOrderForm;
				CurrentEntityChanged(this, args); 
			}
		}
		
		#endregion
		
		/// <summary>
		/// Strongly typed event 
		/// </summary>
		public class SupOrderFormDataGridViewEventArgs : System.EventArgs
		{
			private Entities.SupOrderForm	_SupOrderForm;
	
			/// <summary>
			/// the  Entities.SupOrderForm instance.
			/// </summary>
			public Entities.SupOrderForm SupOrderForm
			{
				get { return _SupOrderForm; }
				set { _SupOrderForm = value; }
			}	
		}
		
		/// <summary>
		/// Handles the DataError event of the uxSupOrderFormDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewDataErrorEventArgs"/> instance containing the event data.</param>
		protected void OnSupOrderFormDataGridViewDataError(object sender, System.Windows.Forms.DataGridViewDataErrorEventArgs e)
		{
			System.Diagnostics.Debug.WriteLine(string.Format( "{0}, {1}", e.ColumnIndex, e.RowIndex));
		}
		
		/// <summary>
		/// Handles the CellValueNeeded event of the uxSupOrderFormDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewCellValueEventArgs"/> instance containing the event data.</param>
		protected void OnSupOrderFormDataGridViewCellValueNeeded(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e)
		{
			switch(uxSupOrderFormDataGridView.Columns[e.ColumnIndex].Name)
			{
					case "uxFormIdDataGridViewColumn":
						e.Value = SupOrderFormList[e.RowIndex].FormId;
						break;
					case "uxBackgroundDataGridViewColumn":
						e.Value = SupOrderFormList[e.RowIndex].Background;
						break;
					case "uxSupplierIdDataGridViewColumn":
						e.Value = SupOrderFormList[e.RowIndex].SupplierId;
						break;
				default:
				break;
			}
		}
		
		/// <summary>
		/// Handles the CellValuePushed event of the uxSupOrderFormDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewCellValueEventArgs"/> instance containing the event data.</param>
		protected void OnSupOrderFormDataGridViewCellValuePushed(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e)
		{
			switch(uxSupOrderFormDataGridView.Columns[e.ColumnIndex].Name)
			{
					case "uxFormIdDataGridViewColumn":
						SupOrderFormList[e.RowIndex].FormId = (System.Int32)e.Value;
						break;
					case "uxBackgroundDataGridViewColumn":
						SupOrderFormList[e.RowIndex].Background = (System.Byte[])e.Value;
						break;
					case "uxSupplierIdDataGridViewColumn":
						SupOrderFormList[e.RowIndex].SupplierId = (System.Int32)e.Value;
						break;
				default:
				break;
			}
		}
	}
}
